﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleKit.FILEIO
{


    public enum FILEIO
    {
        /// <summary>
        /// File IO Successfully
        /// </summary>
        SUCESS,
        /// <summary>
        /// File IO Fail
        /// </summary>
        FAIL,
    };
    public class MyFile
    {

        /// <summary>
        /// Save string into files
        /// </summary>
        /// <param name="saveString"></param>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        public FILEIO SaveStringToFile(string saveString,string fullPath)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(fullPath))
                {
                    sw.Write(saveString);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception)
            {
                return FILEIO.FAIL;
            }

            return FILEIO.SUCESS;
        }
        FILEIO DeleteFolderAndSubChild(string fullPath)
        {
            DirectoryInfo di = new DirectoryInfo(fullPath);
            if (di != null)
            {
                try
                {
                    di.Delete(true);
                    return FILEIO.SUCESS;
                }
                catch (Exception)
                {
                    return FILEIO.FAIL;
                }
            }
           return FILEIO.FAIL;
        }

        //basepath = @"C:\awork\Development\Pacom"
        public void GetAllDirList(string basepath)
        {
            DirectoryInfo di = new DirectoryInfo(basepath);

            DirectoryInfo[] dis = di.GetDirectories();

            for (int i = 0; i < dis.Length; i++)
            {
                /*
                 * 
                 */

                if (dis[i].Name == "ThirdParty")
                    continue;

                if (dis[i].Name == "bin")
                {
                    Console.WriteLine(dis[i].FullName);
                    dis[i].Delete(true);
                    continue;
                }

                GetAllDirList(dis[i].FullName);
            }
        }

        //basepath = @"C:\awork\Development\Pacom"
        public void CopyAllDirList(string basepath)
        {
            pLib.MyFile myFile = new pLib.MyFile();
            DirectoryInfo di = new DirectoryInfo(basepath);

            DirectoryInfo[] dis = di.GetDirectories();

            for (int i = 0; i < dis.Length; i++)
            {
                /*
                 * 
                 */
                if (dis[i].Name == "Debug")
                {
                    Console.WriteLine(dis[i].FullName);
                    //myFile.CopyFile(@"D:\Firebird\Development\Pacom\ThirdParty\Log4Net\log4net.dll", dis[i].FullName+"\\log4net.dll");
                    File.Copy(@"D:\Firebird\Development\Pacom\ThirdParty\Log4Net\log4net.dll", dis[i].FullName + "\\log4net.dll", true);
                    continue;
                }

                CopyAllDirList(dis[i].FullName);
            }
        }



        public static void  DeleteFileMain()
        {
            MyFile df = new MyFile();
            //df.GetAllDirList(@"C:\awork\Development\Pacom");
            df.CopyAllDirList(@"D:\Firebird\Development\Pacom");
        }
    }
}
